package com.example.joblinker.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.joblinker.dto.JobFavoriteDTO;
import com.example.joblinker.entity.JobFavorite;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 职位收藏Mapper接口
 */
@Mapper
public interface JobFavoriteMapper extends BaseMapper<JobFavorite> {

    /**
     * 获取用户收藏的职位列表
     *
     * @param page 分页参数
     * @param userId 用户ID
     * @param keyword 搜索关键词
     * @return 收藏职位列表
     */
    @Select("<script>" +
            "SELECT " +
            "   j.id AS jobId, " +
            "   j.title AS jobTitle, " +
            "   c.name AS companyName, " +
            "   c.logo AS companyLogo, " +
            "   j.salary, " +
            "   j.city, " +
            "   j.tags, " +
            "   jf.create_time AS createTime " +
            "FROM job_favorites jf " +
            "JOIN job j ON jf.job_id = j.id " +
            "JOIN company c ON j.company_id = c.id " +
            "WHERE jf.user_id = #{userId} " +
            "<if test='keyword != null and keyword != \"\"'>" +
            "   AND (j.title LIKE CONCAT('%', #{keyword}, '%') " +
            "        OR c.name LIKE CONCAT('%', #{keyword}, '%')) " +
            "</if>" +
            "ORDER BY jf.create_time DESC" +
            "</script>")
    Page<JobFavoriteDTO> findFavoritesByUserId(Page<?> page, @Param("userId") Long userId, @Param("keyword") String keyword);

    /**
     * 统计用户收藏的职位数量
     *
     * @param userId 用户ID
     * @return 收藏数量
     */
    @Select("SELECT COUNT(*) FROM job_favorites WHERE user_id = #{userId}")
    Integer countByUserId(@Param("userId") Long userId);
} 